package client;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Serializable;
import java.rmi.AccessException;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;

import javax.sql.RowSet;
import javax.sql.rowset.CachedRowSet;



/**
 * Test class used for demonstrating the methods in the back-end DatabaseHandlerImpl
 * 
 * @author Martin, Kristoffer
 *
 */
public class Cmd {
	
	DatabaseHandler dbhand;
	
	/**
	 * Main method that instantiates a new Cmd
	 * 
	 * @param args starting parameters
	 */
	public static void main(String args[]){
		(new Cmd()).startCmd(args);
	}
	
	/**
	 * Method used to start the client
	 * 
	 * @param args starting parameters
	 */
	public void startCmd(String args[]){
		
		try {
			String host = (args.length < 1) ? "localhost" : args[0];
			Registry registry = LocateRegistry.getRegistry(host, 1227);
			dbhand = (DatabaseHandler) registry.lookup("dbImpl");
			
		} catch (AccessException e) {
			
			e.printStackTrace();
		} catch (RemoteException e) {
			
			e.printStackTrace();
		} catch (NotBoundException e) {
			
			e.printStackTrace();
		}
		
		
		
		
		boolean finished = false;
		BufferedReader cin = new BufferedReader(new InputStreamReader(System.in));
		String str = null;
		while(!finished){
			printstart();
			try {
				str =cin.readLine();
			} catch (IOException e) {
				e.printStackTrace();
			}
			handle(str);
		}
	}
	
	private void handle(String str) {
		BufferedReader cin = new BufferedReader(new InputStreamReader(System.in));
		
		/*DatabaseHandlerImpl dbhand = null;
		try {
			dbhand = new DatabaseHandlerImpl();
		} catch (RemoteException e1) {
			e1.printStackTrace();
		}*/
		
		String str1 = null;
		String str2 = null;
		String str3 = null;
		try {
			if (str.equals("1")) {
				System.out.println("Create table");
				System.out.print("Tablename: ");
				str1 = cin.readLine();
				dbhand.createTable(str1);
			} else if (str.equals("2")) {
				System.out.println("Add Column");
				System.out.print("Tablename: ");
				str1 = cin.readLine();
				System.out.print("Columnname: ");
				str2 = cin.readLine();
				System.out.print("Datatype: ");
				str3 = cin.readLine();
				dbhand.createColumn(str1, str2, str3);
			} else if (str.equals("3")) {
				System.out.println("Add data");
				System.out.print("Tablename: ");
				str1 = cin.readLine();
				System.out.println("Number of columns: ");
				int num = Integer.parseInt(cin.readLine());
				HashMap<String, Serializable> hm = new HashMap<String, Serializable>();
				for (int i = 0; i < num;i++) {
					System.out.print("Columnname: ");
					str2 = cin.readLine();
					System.out.print("Data: ");
					str3 = cin.readLine();
					hm.put(str2, str3);	
				}
				dbhand.addData(str1, hm);
			} else if (str.equals("4")) {
				System.out.println("Add blob");
				System.out.print("Tablename: ");
				str1 = cin.readLine();
				System.out.print("Columnname: ");
				str2 = cin.readLine();
				System.out.print("Path: ");
				str3 = cin.readLine();
				File file = new File(str3);
			    FileInputStream fis = new FileInputStream(file);
			    byte[] in = new byte[fis.available()];
			    fis.read(in);
				dbhand.addBLOB(str1,str2, in);
			} else if (str.equals("5")) {
				System.out.println("Show Table");
				System.out.print("Tablename: ");
				str1 = cin.readLine();
				ArrayList<String> alist = dbhand.getData(str1, null, null);
				Iterator<String> it = alist.iterator();
				while(it.hasNext()){
					System.out.println(it.next());
				}
			} else if (str.equals("6")) {
				System.out.println("Show Data");
				System.out.print("Tablename: ");
				str1 = cin.readLine();
				System.out.print("Columnname: ");
				str2 = cin.readLine();
				System.out.print("Equals? : ");
				str3 = cin.readLine();
				ArrayList<String> alist = dbhand.getData(str1, str2, str3);
				Iterator<String> it = alist.iterator();
				while(it.hasNext()){
					System.out.println(it.next());
				}
			} else if (str.equals("7")) {
				System.out.println("Delete Data");
				System.out.print("Tablename: ");
				str1 = cin.readLine();
				System.out.print("Columnname: ");
				str2 = cin.readLine();
				System.out.print("Equals? : ");
				str3 = cin.readLine();
				dbhand.deleteData(str1, str2, str3);
			}else {
				System.out.println("Input not understood!");
			}
		} catch (Exception e) {
			e.printStackTrace();
		
		}
		
	}

	private void printstart(){
		System.out.println();
		System.out.println("Pick a task:");
		System.out.println("Create table ------- 1");
		System.out.println("Add column --------- 2");
		System.out.println("Add data ----------- 3");
		System.out.println("Add blob ----------- 4");
		System.out.println("Show table --------- 5");
		System.out.println("Show data ---------- 6");
		System.out.println("Delete data -------- 7");
		System.out.println();
	}
}
